package com.zr.d_c.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 * 数据库的工具类
 * @author Administrator
 *
 */
public class JDBCUtil {

	private final static String DBURL = "jdbc:mysql://localhost:3306/d_ccrm?useUnicode=true&characterEncoding=utf-8";

	private final static String USERNAME = "root";
	private final static String PASSWORD = "root";

	private final static String DBDRIVER = "com.mysql.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return 连接
	 */
	public static Connection getConnection(){
		Connection con = null;
		try {
			Class.forName(DBDRIVER);
			con = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return con;
	}
	
	/**
	 * 关闭数据库连接
	 * @param pst 要关闭的预编译
	 * @param con 要关闭的连接
	 */
	public static void closeJDBC(PreparedStatement pst, Connection con){
		if(pst != null){
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con != null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
